package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    private R add(@RequestBody ShoppingCart shoppingCart){
        log.info("添加购物车:{}",shoppingCart);
        Long user = BaseContext.getCurrentId();//当前登录用户
        //判断购物车中是否存在该菜品或者套餐
        //查询条件：根据当前用户，菜品id或者套餐id
        LambdaQueryWrapper<ShoppingCart>lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart ::getUserId,user);
        //如果菜品id不为空，说明当前用户添加的是一个菜品，根据菜品id查询购物车
        lqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId, shoppingCart.getDishId());
        //如果套餐id不为空，说明当前用户添加的是一个套餐，根据套餐id查询购物车
        lqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart shoppingCartDB = shoppingCartService.getOne(lqw);

        if (shoppingCartDB!= null){
            //如果存在，数量加1 update
            shoppingCartDB.setNumber(shoppingCartDB.getNumber()+1);
            shoppingCartService.updateById(shoppingCartDB);
            return R.success(shoppingCartDB);
        }else {
            //如果不存在，添加购物车  数量默认为1  insert
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(user);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }


    }
    /**
     * TODO 购物车列表查询接口，先提供一个空方法，防止前台页面报错
     * @return
     */
    @GetMapping("/list")
   public R list() {
        LambdaQueryWrapper<ShoppingCart>lqw = new LambdaQueryWrapper<>();
        //根据当前用户查询购物车
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //根据创建时间升序排序
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> cartList = shoppingCartService.list(lqw);

        return R.success(cartList);
   }


    /**
     * 购物车数量减少
     * @param shoppingCart
     * @return
     */
   @PostMapping("/sub")
   public R sub(@RequestBody ShoppingCart shoppingCart){
       LambdaQueryWrapper<ShoppingCart>queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
       queryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
       ShoppingCart shoppingCartServiceOne= shoppingCartService.getOne(queryWrapper);
       Integer number =shoppingCartServiceOne.getNumber();
        if (number==1){
            shoppingCartService.remove(queryWrapper);
            return R.success("删除成功");
        }else {
            shoppingCartServiceOne.setNumber(shoppingCartServiceOne.getNumber()-1);
            shoppingCartService.updateById(shoppingCartServiceOne);
            return R.success("减少成功");
        }

   }

    /**
     * 清空购物车
     * @return
     */
   @DeleteMapping("clean")
   public R clean(){
       LambdaQueryWrapper<ShoppingCart>lqw = new LambdaQueryWrapper<>();
       //根据当前用户查询购物车
       lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());


       shoppingCartService.remove(lqw);

       return R.success("清空购物车成功");
   }
 
 }   